﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Krile.Forms.ConfigForm.Children
{
    public partial class Colors : Krile.Kernel.Control.ConfigPage
    {
        public Colors()
        {
            InitializeComponent();
        }

        List<AppliableColorConfig> elems = new List<AppliableColorConfig>();
        private void Colors_Load(object sender, EventArgs e)
        {
            //base color configures
            elems.Add(new XCCfg(Lang.ColorConfig.ForeColor, Core.Config.Appearance.BaseColorConfig.ForeColor));
            elems.Add(new XCCfg(Lang.ColorConfig.ForeSecondaryColor, Core.Config.Appearance.BaseColorConfig.ForeSecondaryColor));
            elems.Add(new XCCfg(Lang.ColorConfig.LinkTextColor, Core.Config.Appearance.BaseColorConfig.LinkTextColor));
            elems.Add(new XCCfg(Lang.ColorConfig.LinkHoverTextColor, Core.Config.Appearance.BaseColorConfig.LinkHoverTextColor));
            elems.Add(new XCCfg(Lang.ColorConfig.LinkActiveTextColor, Core.Config.Appearance.BaseColorConfig.LinkActiveTextColor));
            elems.Add(new XCCfg(Lang.ColorConfig.ListBackgroundColor, Core.Config.Appearance.BaseColorConfig.ListBackgroundColor));
            //renderer color config
            elems.Add(new SXCCfg(Lang.ColorConfig.ListAlternateBackgroundColor, Core.Config.Appearance.Renderer.Colors.ListAlternateBackgroundColor));
            elems.Add(new XPCCfg(Lang.ColorConfig.ListReplyColor, Core.Config.Appearance.Renderer.Colors.ListReplyColor));
            elems.Add(new XPCCfg(Lang.ColorConfig.ListReplyNotfoundColor, Core.Config.Appearance.Renderer.Colors.ListReplyNotfoundColor));
            elems.Add(new XPCCfg(Lang.ColorConfig.ListHeaderFooterColor, Core.Config.Appearance.Renderer.Colors.ListHeaderFooterColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListNewestColor, Core.Config.Appearance.Renderer.Colors.ListNewestColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListSelectedUserColor, Core.Config.Appearance.Renderer.Colors.ListSelectedUserColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListReplyToSelectedColor, Core.Config.Appearance.Renderer.Colors.ListReplyToSelectedColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListReplyFromSelectedColor, Core.Config.Appearance.Renderer.Colors.ListReplyFromSelectedColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListReplyToMeColor, Core.Config.Appearance.Renderer.Colors.ListReplyToMeColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListReplyToMeNewestColor, Core.Config.Appearance.Renderer.Colors.ListReplyToMeNewestColor));
            elems.Add(new SXPCCfg(Lang.ColorConfig.ListMyPostColor, Core.Config.Appearance.Renderer.Colors.ListMyPostColor));

            //show init
            elems.Reverse();
            foreach (var i in elems)
            {
                ccParent.Controls.Add(new Panel() { Width = 0, Height = 1, Dock = DockStyle.Top, BackColor = SystemColors.ControlDarkDark });
                var cc = i.GetControl();
                ccParent.Controls.Add(cc);
                cc.Dock = DockStyle.Top;
            }
        }

        public override void Apply()
        {
            foreach (var i in elems)
            {
                i.Apply();
            }
        }

        private abstract class AppliableColorConfig
        {
            public abstract Control GetControl();
            public abstract void Apply();
        }

        private class XCCfg : AppliableColorConfig
        {
            Imported.Data.XColor xc;
            Controls.ColorConfigure cc;
            public XCCfg(string desc, Imported.Data.XColor target)
            {
                xc = target;
                cc = new Krile.Forms.ConfigForm.Children.Controls.ColorConfigure(desc, target.Value);
            }

            public override Control GetControl()
            {
                return cc;
            }

            public override void Apply()
            {
                xc.Value = cc.Value;
            }
        }

        private class SXCCfg : AppliableColorConfig
        {
            Data.SXColor sxc;
            Controls.ColorConfigure cc;
            public SXCCfg(string desc, Data.SXColor target)
            {
                sxc = target;
                cc = new Krile.Forms.ConfigForm.Children.Controls.ColorConfigure(desc, target.Value, target.Enabled);
            }

            public override Control GetControl()
            {
                return cc;
            }

            public override void Apply()
            {
                sxc.Value = cc.Value;
                sxc.Enabled = cc.ColorEnabled;
            }
        }

        private class XPCCfg : AppliableColorConfig
        {
            Imported.Data.XPairColor xpc;
            Controls.PairColorConfigure pcc;
            public XPCCfg(string desc, Imported.Data.XPairColor target)
            {
                xpc = target;
                pcc = new Krile.Forms.ConfigForm.Children.Controls.PairColorConfigure(desc, target.ForeColor, target.BackColor);
            }

            public override Control GetControl()
            {
                return pcc;
            }

            public override void Apply()
            {
                xpc.ForeColor = pcc.ForeColorValue;
                xpc.BackColor = pcc.BackColorValue;
            }
        }

        private class SXPCCfg : AppliableColorConfig
        {
            Data.SXPairColor sxpc;
            Controls.PairColorConfigure pcc;
            public SXPCCfg(string desc, Data.SXPairColor target)
            {
                sxpc = target;
                pcc = new Krile.Forms.ConfigForm.Children.Controls.PairColorConfigure(
                    desc, target.ForeColor, target.BackColor, target.ForeEnabled, target.BackEnabled);
            }

            public override Control GetControl()
            {
                return pcc;
            }

            public override void Apply()
            {
                sxpc.ForeEnabled = pcc.ForeColorEnabled;
                sxpc.ForeColor = pcc.ForeColorValue;
                sxpc.BackEnabled = pcc.BackColorEnabled;
                sxpc.BackColor = pcc.BackColorValue;
            }
        }
    }
}
